import { createReducer } from 'reduxsauce'
import { REHYDRATE } from 'redux-persist/es/constants'
import {
  START_PAIN_MANAGEMENT,
  GET_MY_PAIN_MANAGEMENT,
  SET_DAILY_CONTENT_VIEWED,
  GET_MANAGEMENT_TEMPLATE,
  GET_MANAGEMENT_TEMPLATES,
  GET_PAIN_MANAGEMENT_SUMMARY,
  GET_RECOMMEND_GOODS,
  REDUCES_PLAN_COUPON,
  GET_EVENTS_RECOMMEND_GOODS,
  GET_ALL_HOME_EVENTS,
  GET_EVENTS_HOME_PERSONA,
  GET_EVENTS_HOME_PAGE_SPOT,
  GET_EVENTS_HOME_PAIN_DESC,
  GET_MY_EXPIRED_PAIN_MANAGEMENT_LIST,
  GET_PAIN_MANAGEMENT_RESULT,
  GET_RECOMMEND_PLAN
} from '@ACTIONS/managementPlan'
import { LOGOUT_USER } from '@ACTIONS/auth'
import { PAIN_MANAGEMENT_ACTION } from '@CONSTANTS/common'
const INITIAL_STATE = {
  error: null,
  myPainManagement: {},
  loadingMyPainManagement: true,
  loadingManagementPlanDetail: true,
  loadingHomePageSpotEvents: true,
  managementPlanDetail: null,
  detailHtmlContent: undefined,
  currentDay: undefined,
  templates: [],
  initLoadingTemplates: true,
  planSummary: {},
  recommendGoods: [],
  recommendGoodsEvents: [],
  mainBannerEvents: [],
  homePersonaEvents: [],
  homePageSpotEvents: [],
  homePagePainDescEvents: [],
  myExpiredPainManagementList: [],
  reportDetail: null,
  loadingRecommendPlan: true,
  myRecommendPlan: [],
}

const rehydrate = (state, { payload }) => {
  if (payload && payload.managementPlan) {
    return { ...INITIAL_STATE, ...payload.managementPlan }
  }
  return { ...state }
}

export default createReducer(INITIAL_STATE, {
  [REHYDRATE]: rehydrate,
  [START_PAIN_MANAGEMENT.SUCCESS]: state => {
    const { templates, managementPlanDetail } = state
    const { TemplateId } = managementPlanDetail
    if (!TemplateId) return state
    return {
      ...state,
      managementPlanDetail: { ...managementPlanDetail, Action: PAIN_MANAGEMENT_ACTION.Enter },
      templates: templates.map(item => (item.p8Id === TemplateId ? { ...item, Action: PAIN_MANAGEMENT_ACTION.Enter } : item))
    }
  },
  [START_PAIN_MANAGEMENT.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [GET_MY_PAIN_MANAGEMENT.SUCCESS]: (state, { data }) => ({
    ...state,
    myPainManagement: data || {},
    loadingMyPainManagement: false,
    currentDay: data.CurrentDay || undefined,
  }),
  [GET_MY_PAIN_MANAGEMENT.FAILURE]: (state, { error }) => ({
    ...state,
    loadingMyPainManagement: false,
    error
  }),
  [GET_MANAGEMENT_TEMPLATE.REQUEST]: state => ({
    ...state,
    managementPlanDetail: null,
    loadingManagementPlanDetail: true
  }),
  [GET_MANAGEMENT_TEMPLATE.SUCCESS]: (state, { data, htmlContent }) => ({
    ...state,
    managementPlanDetail: data,
    loadingManagementPlanDetail: false,
    detailHtmlContent: htmlContent,
  }),
  [GET_MANAGEMENT_TEMPLATE.FAILURE]: (state, { error }) => ({
    ...state,
    loadingManagementPlanDetail: false,
    error
  }),
  [SET_DAILY_CONTENT_VIEWED.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [GET_MANAGEMENT_TEMPLATES.REQUEST]: state => ({
    ...state,
    initLoadingTemplates: true
  }),
  [GET_MANAGEMENT_TEMPLATES.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [GET_MANAGEMENT_TEMPLATES.SUCCESS]: (state, { data }) => ({
    ...state,
    initLoadingTemplates: false,
    templates: data,
  }),
  [GET_PAIN_MANAGEMENT_SUMMARY.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [GET_PAIN_MANAGEMENT_SUMMARY.SUCCESS]: (state, { data }) => ({
    ...state,
    planSummary: data,
  }),
  [GET_RECOMMEND_GOODS.SUCCESS]: (state, { data }) => ({
    ...state,
    recommendGoods: data,
  }),
  [GET_MY_EXPIRED_PAIN_MANAGEMENT_LIST.SUCCESS]: (state, { data }) => ({
    ...state,
    myExpiredPainManagementList: data,
  }),
  [GET_PAIN_MANAGEMENT_RESULT.SUCCESS]: (state, { result, pId }) => {
    const { myExpiredPainManagementList } = state
    const newList = [...myExpiredPainManagementList]
    const idx = myExpiredPainManagementList.findIndex(item => item.PainManagementId === pId && !item.ReportViewed)
    if (idx !== -1) {
      const cur = newList[idx]
      newList[idx] = { ...cur, ReportViewed: true }
    }
    return ({
      ...state,
      reportDetail: result,
      myExpiredPainManagementList: newList
    })
  },
  [GET_PAIN_MANAGEMENT_RESULT.REQUEST]: state => ({
    ...state,
    reportDetail: null,
  }),
  [REDUCES_PLAN_COUPON.REQUEST]: (state, { id }) => {
    if (!id) return state
    const prePainData = state.myPainManagement
    const preId = prePainData?.PainManagementId
    const newPainData = prePainData
    if (preId === id) {
      const { PicTextConsultation } = prePainData
      const newPicTextConsultation = { ...PicTextConsultation }
      newPicTextConsultation.ConsumedQuantity = PicTextConsultation.ConsumedQuantity + 1
      newPainData.PicTextConsultation = newPicTextConsultation
    }
    return {
      ...state,
      myPainManagement: newPainData
    }
  },
  [GET_EVENTS_RECOMMEND_GOODS.SUCCESS]: (state, { data }) => ({
    ...state,
    recommendGoodsEvents: data,
  }),
  [GET_ALL_HOME_EVENTS.SUCCESS]: (state, { data }) => ({
    ...state,
    mainBannerEvents: data,
  }),
  [GET_EVENTS_HOME_PERSONA.SUCCESS]: (state, { data }) => ({
    ...state,
    homePersonaEvents: data,
  }),
  [GET_EVENTS_HOME_PAGE_SPOT.SUCCESS]: (state, { data }) => ({
    ...state,
    loadingHomePageSpotEvents: false,
    homePageSpotEvents: data,
  }),
  [GET_EVENTS_HOME_PAIN_DESC.SUCCESS]: (state, { data }) => ({
    ...state,
    homePagePainDescEvents: data,
  }),
  [GET_RECOMMEND_PLAN.REQUEST]: state => ({
    ...state,
    loadingRecommendPlan: true
  }),
  [GET_RECOMMEND_PLAN.SUCCESS]: (state, { data }) => ({
    ...state,
    myRecommendPlan: data,
    loadingRecommendPlan: false,
  }),
  [LOGOUT_USER.SUCCESS]: state => ({
    ...state,
    myPainManagement: {},
    planSummary: {},
  }),
})
